from common.lib_mysql import News


class NewsBody(News):
    def __init__(self):
        super().__init__()
        self.table = "users_newsbody"

    def list(self, page, search_key, new_type, user_id):
        where_sql = list()
        search_sql = 'title like "%{}%" or username like "%{}%"'.format(search_key, search_key)
        if search_key:
            where_sql.append(search_sql)
        if new_type:
            where_sql.append("new_type = '{}'".format(new_type))
        if user_id != -1:
            where_sql.append("user_id = {}".format(user_id))
        return self.list_func_with_all(self.table, page, where_sql, "news")

    def delete(self, ids):
        return self.delete_func(self.table, ids)

    def update(self, id, up_dict):
        return self.update_by_id_func(self.table, [id], up_dict)

    def insert(self, insert_dict):
        return self.insert_func(self.table, insert_dict)

    def find_one_by_id(self, id):
        return self.find_one_by_id_func(self.table, id)
